package org.lanqiao.tjut.Controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.tjut.Bean.TeacherBean;
import org.lanqiao.tjut.MyUtils.MyUtils;
import org.lanqiao.tjut.Service.ServiceFactory;

public class TeaInfoModifyServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 准备用户进行修改的数据
		String strOperationType = request.getParameter("opt");
		int iOpt = MyUtils.obj2Int(strOperationType);
		// 结果跳转页面
		String strJumpPage = "";

		if (iOpt == 0) {
			// 修改跳转操作
			// 获取主键ID
			String strUid = request.getParameter("uid");
			TeacherBean teaB = ServiceFactory.getTeacherInfoManagerServiceInstance().getTeaInfoById(strUid);
			// 将数据缓存在session对象中
			request.setAttribute("teaB", teaB);

			// 修改性别输入框为选择框

			// 跳转到修改结果页
			strJumpPage = "logined/UserInfoManager/teacherInfoAdd.jsp";
		} else if (iOpt == 1) {
			// 删除

			int teaId = MyUtils.obj2Int(request.getParameter("uid"));
			// 数据库删除
			int i = ServiceFactory.getTeacherInfoManagerServiceInstance().deleteTeaById(teaId);
			// 跳转页面
			strJumpPage = "TeaInfoModifyServlet?opt=2";
		} else if (iOpt == 2) {
			// 判定当前的操作是查询还是修改跳转
			// 查询操作
			// 2、将保单数据填充到实体类
			TeacherBean teaB = (TeacherBean) MyUtils.getNewInstance(TeacherBean.class);
			teaB.setTeaname(request.getParameter("txtTeaName"));
			teaB.setTeaage(MyUtils.obj2Int(request.getParameter("txtTeaAge")));
			teaB.setTeaage2(MyUtils.obj2Int(request.getParameter("txtTeaAge2")));
			teaB.setTeaaddress(request.getParameter("txtTeaAddress"));
			teaB.setTeasex(request.getParameter("selectTeaSex"));
			teaB.setTeatel(request.getParameter("txtTeaTel"));
			// 查询操作
			List<TeacherBean> lstTea = ServiceFactory.getTeacherInfoManagerServiceInstance().doQueryByParas(teaB);
			// 将需要显示的结果集放入session缓存中
			request.getSession().setAttribute("lstTea", lstTea);
			// 查询结果页
			strJumpPage = "logined/UserInfoManager/teaInfoDetail.jsp";
		}
		// 跳转到修改页面
		request.getRequestDispatcher(strJumpPage).forward(request, response);
	}

}
